﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BenefitHelper.View
{
    public class OperatorYearTick
    {
        public int Id { get; set; }
        /// <summary>
        /// 账户
        /// </summary>
        public int OperatorId { get; set; }

        public virtual Sys.Operator Operator { get; set; }

        public int Year { get; set; }

        /// <summary>
        /// 是否亏损
        /// </summary>
        public bool Win { get; set; }
        /// <summary>
        /// 发生TICK值
        /// </summary>
        public int Tick { get; set; }
        /// <summary>
        /// 次数
        /// </summary>
        public int Count { get; set; }
        /// <summary>
        /// 合约编号
        /// </summary>
        public int InstrumentId { get; set; }
        /// <summary>
        /// 多空方向
        /// </summary>
        public int Direction { get; set; }
        /// <summary>
        /// 得到操作员年盈利或亏损的Tick数量
        /// </summary>
        /// <param name="operatorid"></param>
        /// <param name="tradeHistoryId"></param>
        /// <param name="win"></param>
        /// <returns></returns>
        public List<OperatorYearTick> GetOperatorYearWinOrLostTicks(int operatorid, int year, bool win, DB.DBManager db)
        {
            var query = db.OperatorYearTick.Where(a => a.OperatorId == operatorid).Where(a => a.Year == year).Where(a => a.Win == win).OrderBy(a => a.Tick);
            List<OperatorYearTick> returnValues = new List<OperatorYearTick>();

            for (int i = 1; i < 11; i++)
            {
                OperatorYearTick tick = new OperatorYearTick();
                tick.Tick = i;
                var query1 = query.Where(a => a.Tick == tick.Tick);
                if (query1 == null || query1.Count() == 0)
                {
                    tick.Count = 0;
                }
                else
                {
                    tick.Count = query1.Sum(a => a.Count);
                }

                returnValues.Add(tick);
                tick = null;
            }
            OperatorYearTick tick2 = new OperatorYearTick();
            tick2.Tick = 11;
            var query2 = query.Where(a => a.Count > 10);
            if (query2.Count() > 0)
            {
                tick2.Count = query2.Sum(a => a.Count);
            }
            else
            {
                tick2.Count = 0;
            }

            returnValues.Add(tick2);
            return returnValues;
        }

        public void InitOperatorYearTick(int year, DB.DBManager db, bool win)
        {
            List<OperatorYearTick> ticks = new List<OperatorYearTick>();
            List<Sys.Operator> operators = new Sys.Operator().GetUnDeletedOperators(db);
            foreach (Sys.Operator op in operators)
            {
                var query = db.OperatorMonthTick.Where(a => a.OperatorId == op.Id).Where(a => a.Year == year).ToList();

                if (query.Count() > 0)
                {
                    foreach (BenefitHelper.View.OperatorMonthTick t in query)
                    {
                        var queryExist = ticks.Where(a => a.OperatorId == op.Id).Where(a => a.Direction == t.Direction).Where(a => a.InstrumentId.Equals(t.InstrumentId)).Where(a => a.Win == t.Win).Where(a => a.Tick == t.Tick).ToList();
                        if (queryExist.Count() == 0)
                        {
                            var querySum = query.Where(a => a.Direction == t.Direction).Where(a => a.InstrumentId.Equals(t.InstrumentId)).Where(a => a.Win == t.Win).Where(a => a.Tick == t.Tick).ToList();
                            BenefitHelper.View.OperatorYearTick tick = new OperatorYearTick();
                            tick.Count = querySum.Sum(a => a.Count);
                            tick.Direction = t.Direction;
                            tick.InstrumentId = t.InstrumentId;
                            tick.OperatorId = op.Id;
                            tick.Tick = t.Tick;
                            tick.Win = t.Win;
                            tick.Year = year;
                            ticks.Add(tick);
                        }
                    }
                }
            }
            foreach (View.OperatorYearTick tick in ticks)
            {
                var queryExist = db.OperatorYearTick.Where(a => a.OperatorId == tick.OperatorId).Where(a => a.Direction == tick.Direction).Where(a => a.InstrumentId.Equals(tick.InstrumentId)).Where(a => a.Tick == tick.Tick).Where(a => a.Win == tick.Win).Where(a => a.Year == tick.Year);
                if (queryExist.Count() > 0)
                {
                    queryExist.First().Count = tick.Count;
                }
                else
                {
                    db.OperatorYearTick.Add(tick);
                }
            }

            db.SaveChanges();
        }

        public void Delete(int year, DB.DBManager db)
        {
            var query = db.OperatorYearTick.Where(a => a.Year == year);
            foreach (OperatorYearTick t in query)
            {
                db.OperatorYearTick.Remove(t);
            }
            db.SaveChanges();
        }
    }
}
